<?php defined('SYSPATH') or die('No direct access allowed.');
class lib_navigate{
	
	public $navigates = array(
		//0=>array('name'=>'test','link'=>'http://test')
	);
	
	private $common_auto_a,$first_auto_a,$last_auto_a;
	
	private $common_a_attr,$first_a_attr,$last_a_attr;
	
	private $common_tag,$first_tag,$last_tag;
	
	public function __construct(){
		$this->common_auto_a = true;
		$this->common_a_attr = array();
	}
	
	public function add($name,$link = null){
		if (is_string($link))
			$this->navigates[] = array('name'=>$name,'link'=>$link);
		else
			$this->navigates[] = array('name'=>$name);
		return $this;
	}
	
	public function insert($name,$link = null){
		$last = array_pop($this->navigates);
		if (is_string($link))
			$this->navigates[] = array('name'=>$name,'link'=>$link);
		else
			$this->navigates[] = array('name'=>$name);
		$this->navigates[] = $last;
		return $this;
	}
	
	public function clear(){
		$this->navigates = array();
		return $this;
	}
	
	public function a($attributes,$auto_a = true){
		$this->common_a_attr = array_filter((array)$attributes);
		$this->common_auto_a = $auto_a;
		return $this;
	}
	
	public function first_a($attributes,$auto_a = true){
		$this->first_a_attr = array_filter((array)$attributes);
		$this->first_auto_a = $auto_a;
		return $this;
	}
	
	public function last_a($attributes,$auto_a = true){
		$this->last_a_attr = array_filter((array)$attributes);
		$this->last_auto_a = $auto_a;
		return $this;
	}
	
	//wrap=false && cover=true; wrap=true && cover=false;
	public function tag($tag,$attributes = null,$wrap = false){
		$this->common_tag = array(
			'tag'=>(string)$tag,
			'attr'=>array_filter((array)$attributes),
			'wrap'=>(bool)$wrap
		);
		return $this;
	}
	
	public function first_tag($tag,$attributes = null,$wrap = false){
		$this->first_tag = array(
			'tag'=>(string)$tag,
			'attr'=>array_filter((array)$attributes),
			'wrap'=>(bool)$wrap
		);
		return $this;
	}
	
	public function last_tag($tag,$attributes = null,$wrap = false){
		$this->last_tag = array(
			'tag'=>(string)$tag,
			'attr'=>array_filter((array)$attributes),
			'wrap'=>(bool)$wrap
		);
		return $this;
	}
	
	private function common_born($navigate){
		return $this->born('common',$navigate);
	}
	
	private function first_born($navigate){
		if (!isset($this->first_tag) && !isset($this->first_auto_a))
			return $this->born('common',$navigate);
		elseif (!isset($this->first_auto_a))
			list($this->first_auto_a,$this->first_a_attr) = array($this->common_auto_a,$this->common_a_attr);
		return $this->born('first',$navigate);
	}
	
	private function last_born($navigate){
		if (!isset($this->last_tag) && !isset($this->last_auto_a))
			return $this->born('common',$navigate);
		elseif (!isset($this->last_auto_a))
			list($this->last_auto_a,$this->last_a_attr) = array($this->common_auto_a,$this->common_a_attr);
		return $this->born('last',$navigate);
	}
	
	private function born($eq = 'common',$navigate){
		$tag = &$this->{$eq.'_tag'};
		$auto_a = &$this->{$eq.'_auto_a'};
		$a_attr = &$this->{$eq.'_a_attr'};
		
		if (!isset($tag) && !$auto_a)
			return $navigate['name'];
		if (!isset($tag) && $auto_a && isset($navigate['link']))
			return HTML::anchor($navigate['link'],$navigate['name'],$a_attr);
		if (isset($tag) && !$tag['wrap'])
			return '<'.$tag['tag'].HTML::attributes($tag['attr']).'>'.$navigate['name'].'</'.$tag['tag'].'>';
		if (isset($tag) && $tag['wrap'] && $auto_a && isset($navigate['link']))
			return '<'.$tag['tag'].HTML::attributes($tag['attr']).'>'
			.HTML::anchor($navigate['link'],$navigate['name'],$a_attr)
			.'</'.$tag['tag'].'>';
		if (isset($tag) && $tag['wrap'] && (!$auto_a || !isset($navigate['link'])))
			return '<'.$tag['tag'].HTML::attributes($tag['attr']).'>'.$navigate['name'].'</'.$tag['tag'].'>';
		return $navigate['name'];
	}
	
	public function implode($glue){
		$navigates = array();
		if (!empty($this->navigates))
			$first = array_shift($this->navigates);
		if (!empty($this->navigates))
			$last = array_pop($this->navigates);
		
		if (isset($first))
			$navigates[] = $this->first_born($first);
		if (!empty($this->navigates))
			foreach ($this->navigates as $navigate){
				$navigates[] = $this->common_born($navigate);
			}
		if (isset($last))
			$navigates[] = $this->last_born($last);
		
		return implode($glue,$navigates);
	}
}